#include "VWords.h"
#include <QDebug>

VWords::VWords(QString name, QString type_name, QString signature, int line, int kind)
{
    _name = name;
    _type_name = type_name;
    _signature = signature;

    _line = line;
    _kind = kind;
}

VWords::VWords(VWords *source)
{
    _name = source->_name;
    _type_name = source->_type_name;
    _signature = source->_signature;

    _line = source->_line;
    _kind = source->_kind;

    foreach (VWords* p_word, source->_list)
    {
        _list.append(new VWords(p_word));
    }
}


VWords::~VWords()
{
    foreach (VWords* obj, _list)
	{
		delete obj;
    }
}

void VWords::clear()
{
    foreach (VWords* obj, _list)
    {
        delete obj;
    }

    _list.clear();
}

void VWords::append(VWords *child)
{
    _list.append(child);
}

bool VWords::matching(QString text, int kind, int match, int line, int mode)
{
    bool result;
//    qDebug() << "this.name:" << _name << "text: " << text;
    if(text.isEmpty())
    {
        return true;
    }

//    if(text == _name)
//    {
//        qDebug() << 11;
//    }

    QString source;
    switch (match) {
    case MATCH::NAME:
        source = this->_name;
        break;
    case MATCH::TYPENAME:
        source = this->_type_name;
        break;
    }

    switch (mode) {
    case MODE::PART:
        result = ((line == -1 || line >= this->_line) &&
                VFunctions::matching_string(source, text) &&
                (this->_kind & kind) == this->_kind);
//        qDebug() << result;
        return result;
        break;
    case MODE::ALL:
        result =  ((line == -1 || line >= this->_line) &&
                source == text &&
                (this->_kind & kind) == this->_kind);
//        qDebug() << result;
        return result;
        break;
    default:
        return false;
        break;
    }
}



VWords * VWords::matching_child(QString text, int kind, int match, int line, int mode)
{
    foreach(VWords *obj, _list)
    {
        if(obj->matching(text, kind, match, line, mode))
        {
            return obj;
        }
    }
    return nullptr;
}

QList<VWords *> VWords::matching_childs(QString text, int kind, int match, int line, int mode)
{
    QList<VWords *> result;
    foreach(VWords *obj, _list)
    {
        if(obj->matching(text, kind, match, line, mode))
        {
            result.append(obj);
        }
    }
    return result;
}

void VWords::set_file(void *file)
{
    this->_file = file;

    foreach(VWords *obj, _list)
    {
        obj->set_file(file);
    }
}


